package com.example.yichuangyun.mvvm.ui.adapter;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.yichuangyun.R;
import com.example.yichuangyun.databinding.ItemRiskChildBinding;
import com.example.yichuangyun.databinding.ItemRiskInfoBinding;
import com.example.yichuangyun.model.company.ManageInfoDataModel;
import com.example.yichuangyun.model.company.RiskInfoDataModel;
import com.example.yichuangyun.utils.CommonUtil;

import java.util.List;

/**
 * 经营异常
 */
public class ManageInfoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final Context context;
    private final List<ManageInfoDataModel> data;

    public ManageInfoAdapter(Context context, List<ManageInfoDataModel> data) {
        this.context = context;
        this.data = data;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemRiskInfoBinding binding = ItemRiskInfoBinding.inflate(LayoutInflater.from(context), parent, false);
        return new MyViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            MyViewHolder myViewHolder = (MyViewHolder) holder;
            ManageInfoDataModel model = data.get(position);
            switch (position) {
                case 0: // 税务评级
                    myViewHolder.binding.tvCount.setText(context.getString(R.string.tab_count, model.getPayTaxes().size()));
                    myViewHolder.binding.recyclerView.setAdapter(new MyAdapter(model.getPayTaxes(), position));
                    myViewHolder.binding.ivArrow.setVisibility(model.getPayTaxes().size() > 0 ? View.VISIBLE : View.GONE);
                    myViewHolder.binding.tvNoData.setVisibility(model.getPayTaxes().size() > 0 ? View.GONE : View.VISIBLE);
                    myViewHolder.binding.tvCount.setVisibility(model.getPayTaxes().size() > 0 ? View.VISIBLE : View.GONE);
                    break;
                case 1: // 资质证书
                    myViewHolder.binding.tvCount.setText(context.getString(R.string.tab_count, model.getCertificates().size()));
                    myViewHolder.binding.recyclerView.setAdapter(new MyAdapter(model.getCertificates(), position));
                    myViewHolder.binding.ivArrow.setVisibility(model.getCertificates().size() > 0 ? View.VISIBLE : View.GONE);
                    myViewHolder.binding.tvNoData.setVisibility(model.getCertificates().size() > 0 ? View.GONE : View.VISIBLE);
                    myViewHolder.binding.tvCount.setVisibility(model.getCertificates().size() > 0 ? View.VISIBLE : View.GONE);
                    break;
                case 2: // 行政许可
                    myViewHolder.binding.tvCount.setText(context.getString(R.string.tab_count, model.getLicenseInfos().size()));
                    myViewHolder.binding.recyclerView.setAdapter(new MyAdapter(model.getLicenseInfos(), position));
                    myViewHolder.binding.ivArrow.setVisibility(model.getLicenseInfos().size() > 0 ? View.VISIBLE : View.GONE);
                    myViewHolder.binding.tvNoData.setVisibility(model.getLicenseInfos().size() > 0 ? View.GONE : View.VISIBLE);
                    myViewHolder.binding.tvCount.setVisibility(model.getLicenseInfos().size() > 0 ? View.VISIBLE : View.GONE);
                    break;
                case 3: // 一般纳税人
                    myViewHolder.binding.tvCount.setText(context.getString(R.string.tab_count, model.getGeneralTaxpayers().size()));
                    myViewHolder.binding.recyclerView.setAdapter(new MyAdapter(model.getGeneralTaxpayers(), position));
                    myViewHolder.binding.ivArrow.setVisibility(model.getGeneralTaxpayers().size() > 0 ? View.VISIBLE : View.GONE);
                    myViewHolder.binding.tvNoData.setVisibility(model.getGeneralTaxpayers().size() > 0 ? View.GONE : View.VISIBLE);
                    myViewHolder.binding.tvCount.setVisibility(model.getGeneralTaxpayers().size() > 0 ? View.VISIBLE : View.GONE);
                    break;
            }
            myViewHolder.binding.recyclerView.setVisibility(model.isOpen() ? View.VISIBLE : View.GONE);
            myViewHolder.binding.ivArrow.setImageResource(model.isOpen() ? R.mipmap.icon_up : R.mipmap.icon_down);
            myViewHolder.binding.tvTitle.setText(model.getTitle());

            myViewHolder.binding.llTitle.setEnabled(data.size() > 0);
            myViewHolder.binding.llTitle.setOnClickListener(view -> {
                model.setOpen(!model.isOpen());
                notifyItemChanged(holder.getAbsoluteAdapterPosition());
            });
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        ItemRiskInfoBinding binding;

        public MyViewHolder(ItemRiskInfoBinding itemRiskInfoBinding) {
            super(itemRiskInfoBinding.getRoot());
            binding = itemRiskInfoBinding;
        }
    }

    class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private final List<ManageInfoDataModel.ManageInfoModel> mData;
        private final int parentPosition;

        public MyAdapter(List<ManageInfoDataModel.ManageInfoModel> mData, int parentPosition) {
            this.mData = mData;
            this.parentPosition = parentPosition;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            ItemRiskChildBinding binding = ItemRiskChildBinding.inflate(LayoutInflater.from(context), parent, false);
            return new ManageViewHolder(binding);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            if (holder instanceof ManageViewHolder) {
                ManageViewHolder myViewHolder = (ManageViewHolder) holder;
                ManageInfoDataModel.ManageInfoModel model = mData.get(position);
                myViewHolder.binding.tvText5.setVisibility(parentPosition == 0 ? View.GONE : View.VISIBLE);
                myViewHolder.binding.tvText6.setVisibility(parentPosition == 0 || parentPosition == 3 ? View.GONE : View.VISIBLE);
                switch (parentPosition) {
                    case 0: // 税务评级
                        myViewHolder.binding.tvText1.setText(Html.fromHtml(context.getString(R.string.evaluation_year, CommonUtil.setText3(model.getYear())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText2.setText(Html.fromHtml(context.getString(R.string.pay_taxes_level), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText3.setText(Html.fromHtml(context.getString(R.string.pay_taxes_name, CommonUtil.setText3(model.getName())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText4.setText(Html.fromHtml(context.getString(R.string.pay_taxes_no, CommonUtil.setText3(model.getNumber())), Html.FROM_HTML_MODE_LEGACY));
                        break;
                    case 1: // 资质证书
                        myViewHolder.binding.tvText1.setText(Html.fromHtml(context.getString(R.string.certificate_no, CommonUtil.setText3(model.getRegisterNo())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText2.setText(Html.fromHtml(context.getString(R.string.certificate_type, CommonUtil.setText3(model.getType())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText3.setText(Html.fromHtml(context.getString(R.string.certificate_name, CommonUtil.setText3(model.getLicenseName())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText4.setText(Html.fromHtml(context.getString(R.string.certificate_start_date, CommonUtil.setText3(model.getValidStart())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText5.setText(Html.fromHtml(context.getString(R.string.licence_end_date, CommonUtil.setText3(model.getValidEnd())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText6.setText(Html.fromHtml(context.getString(R.string.certificate_status, CommonUtil.setText3(model.getState())), Html.FROM_HTML_MODE_LEGACY));
                        break;
                    case 2: // 行政许可
                        myViewHolder.binding.tvText1.setText(Html.fromHtml(context.getString(R.string.licence_no, CommonUtil.setText3(model.getNumber())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText2.setText(Html.fromHtml(context.getString(R.string.licence_name, CommonUtil.setText3(model.getName())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText3.setText(Html.fromHtml(context.getString(R.string.licence_start_date, CommonUtil.setText3(model.getStartDate())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText4.setText(Html.fromHtml(context.getString(R.string.licence_end_date, CommonUtil.setText3(model.getEndDate())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText5.setText(Html.fromHtml(context.getString(R.string.licensing_org, CommonUtil.setText3(model.getDepartment())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText6.setText(Html.fromHtml(context.getString(R.string.licence_source, CommonUtil.setText3(model.getSourceDepartment())), Html.FROM_HTML_MODE_LEGACY));
                        break;
                    case 3: // 一般纳税人
                        myViewHolder.binding.tvText1.setText(Html.fromHtml(context.getString(R.string.pay_taxes_name, CommonUtil.setText3(model.getName())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText2.setText(Html.fromHtml(context.getString(R.string.pay_taxes_no, CommonUtil.setText3(model.getTaxNum())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText3.setText(Html.fromHtml(context.getString(R.string.pay_taxes_type, CommonUtil.setText3(model.getQualification())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText4.setText(Html.fromHtml(context.getString(R.string.manager_org, CommonUtil.setText3(model.getManageOrgan())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText5.setText(Html.fromHtml(context.getString(R.string.believe_date, CommonUtil.setText3(model.getJudgeDate())), Html.FROM_HTML_MODE_LEGACY));
                        break;
                }
            }
        }

        @Override
        public int getItemCount() {
            return mData.size();
        }
    }

    static class ManageViewHolder extends RecyclerView.ViewHolder {

        ItemRiskChildBinding binding;

        public ManageViewHolder(ItemRiskChildBinding itemRiskChildBinding) {
            super(itemRiskChildBinding.getRoot());
            binding = itemRiskChildBinding;
        }
    }
}
